import { Component, Input, OnInit } from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { ApplicationService } from '@core/routes-services/application.service';
import { APPLICATION_TYPE_OPTION } from '../../../constant/dictionary';
import { AgentRechargeComponent } from '../../agent/agent-list/agent-recharge/agent-recharge.component';
import { AuditingComponent } from '../../auditing/auditing.component';
import { AgentService } from '@core/routes-services/agent.service';

@Component({
  selector: 'application-list',
  templateUrl: './application-list.component.html',
})
export class ApplicationListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  agentId:any;
  @Input() status: any = 1;
  @Input() allAgent: any [] = [];
  @Input() levelOptions: any [] = [];
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };


  typeOption:any [] = APPLICATION_TYPE_OPTION;
  constructor(private message: NzMessageService,
              private router: Router,
              private applicationService: ApplicationService,
              private agentService:AgentService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }


  initPage() {
    this.isLoading = true;
    this.applicationService.pagination(this.page, this.keyword,this.status,this.agentId).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  agentRecharge = (entity) => {
    let agent = this.allAgent.find((t)=>t.id == entity.agentId);
    this.modalHelper.open(AgentRechargeComponent, { entity: agent,applicationId:entity.id,applicationIn:entity.amount, levelOptions: this.levelOptions }, 560, {
      nzTitle: '渠道充值',
    }).subscribe(result => {
      if(result){
        this.message.success("充值成功")
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  reject = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(AuditingComponent, { entity: bak,businessService: this.agentService}, 360, {
      nzTitle: '驳回充值请求',
    }).subscribe(result => {
      if(result){
        this.message.success("驳回成功")
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };


  picList:any [] = [];
  isShowPic:boolean = false;
  showPic(key){
    this.picList = [];
    this.picList.push({imgKey:key});
    this.isShowPic = true
  }
  onPicClose(res){
    this.isShowPic = res
    this.isShowPic = false
  }

}
